//
//  LoginTableViewCell.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/15.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "LoginTableViewCell.h"

static NSString * name_Str;
static NSString * pass_Str;

static GLGWView * linePhone_View;
static GLGWView * linePassword_View;

@interface LoginTableViewCell ()
{
    NSInteger i;//定义全局变量
    BOOL showPassword;
    GLGWTextField * loginInputTextField;
}
@end

@implementation LoginTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier theLoginRow:(NSInteger)row_num{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        GLGWView * lineView = [[GLGWView alloc] initWithInSuperView:self.contentView];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(30));
            make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(30));
            make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
            make.height.equalTo(@1);
        }];
        //
        i = 0;
        loginInputTextField = [[GLGWTextField alloc] initWithFrame:CGRectMake(kWidthProportion_In_iPhoneTriploid(30), kHeightProportion_In_iPhoneTriploid(10), KScreenWidth - kWidthProportion_In_iPhoneTriploid(100), kHeightProportion_In_iPhoneTriploid(52)) titlePlaceholder:@"输入密码" theFont:SYSTEMFONT(16) theTitColor:kColor_Rule_Black inSuperView:self];
        loginInputTextField.tag = row_num + 140;
        loginInputTextField.delegate = nil;
        loginInputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
        [loginInputTextField addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
        [loginInputTextField addTarget:self action:@selector(textField1TextBegin:) forControlEvents:UIControlEventEditingDidBegin];
        //
        if (row_num == 1) {
            linePassword_View = lineView;
            loginInputTextField.secureTextEntry = YES;
            loginInputTextField.keyboardType = UIKeyboardTypeASCIICapable;
            GLGWButton * seePosswordBut = [[GLGWButton alloc] initInSuperView:self.contentView];
            seePosswordBut.image_Name = @"icon_zhengyanjing";
            [seePosswordBut setImage:[UIImage imageNamed:@"icon_biyanjing"] forState:UIControlStateSelected];
            [seePosswordBut addTarget:self action:@selector(seePassword:) forControlEvents:UIControlEventTouchUpInside];
            [seePosswordBut mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
                make.top.equalTo(self.contentView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(13));
                make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(50)));
                make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(46)));
            }];
        }else{
            linePhone_View = lineView;
            loginInputTextField.placeholder = @"输入手机号";
            loginInputTextField.keyboardType = UIKeyboardTypeNumberPad;
        }
        
    }
    return self;
}

-(void)seePassword:(GLGWButton *)sender{
    sender.selected = !sender.selected;
    loginInputTextField.secureTextEntry = !sender.selected;
}

-(void)textField1TextChange:(GLGWTextField *)textField{
    if (textField.tag == 140) {//手机号码
        name_Str = textField.text;
        if (textField.text.length > i) {
            if (textField.text.length == 4 || textField.text.length == 9 ) {//输入
                NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
                [str insertString:@" " atIndex:(textField.text.length-1)];
                textField.text = str;
            }if (textField.text.length >= 13 ) {//输入完成
                textField.text = [textField.text substringToIndex:13];
                [textField resignFirstResponder];
            }
            i = textField.text.length;
            
        }else if (textField.text.length < i){//删除
            if (textField.text.length == 4 || textField.text.length == 9) {
                textField.text = [NSString stringWithFormat:@"%@",textField.text];
                textField.text = [textField.text substringToIndex:(textField.text.length-1)];
            }
            i = textField.text.length;
        }
    }else{//密码
        pass_Str = textField.text;
    }
//    DLog(@"%@-----------%@",name_Str,pass_Str);
    //原始字符串，全部剔除空格
    NSString *originNoSpaceString = [name_Str stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (self.delegate && [self.delegate respondsToSelector:@selector(inputNameStr:thePassStr:)]) {
        [self.delegate inputNameStr:originNoSpaceString thePassStr:pass_Str];
    }
}

-(void)textField1TextBegin:(GLGWTextField *)textField{
    if (textField.tag == 140) {
        linePhone_View.backgroundColor = kColor_Rule_Yellow;
        linePassword_View.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    }else{
        linePassword_View.backgroundColor = kColor_Rule_Yellow;
        linePhone_View.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
